<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesList" border>
        <el-table-column type="expand" label="展开" prop="" width="60px">
          <template slot-scope="scope">
            <el-row
              style="background: #eee; margin-bottom: 20px; padding: 20px"
              v-for="item1 in scope.row.children"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag
                  type="primary"
                  closable
                  @close="removeRights(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
              </el-col>
              <el-col :span="19">
                <el-row
                  style="
                    border: 1px solid orange;
                    margin-bottom: 20px;
                    padding: 20px;
                  "
                  v-for="item2 in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="5">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRights(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                  </el-col>
                  <el-col :span="19">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRights(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showSetRight(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightdialogVisible"
      width="30%"
      @close="setRightDialogClosed"
    >
      <el-tree
        :data="rightList"
        :props="defaultProps"
        default-expand-all
        show-checkbox
        node-key="id"
        :default-checked-keys="defkeys"
        ref="mytree"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="alloRight">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Roles',
  data () {
    return {
      rolesList: [],
      setRightdialogVisible: false,
      rightList: [],
      defaultProps: {
        label: 'authName'
      },
      defkeys: [],
      roleId: null
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.rolesList = res.data
      // console.log(this.rolesList)
    },
    removeRights (row, id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`roles/${row.id}/rights/${id}`)
        if (res.meta.status !== 200) return this.$message.error('获取数据失败')
        row.children = res.data
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async showSetRight (row) {
      this.roleId = row.id
      this.setRightdialogVisible = true
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.rightList = res.data

      var keys = []
      function fn (obj) {
        obj.children.forEach(item => {
          if (item.children) {
            fn(item)
          } else {
            keys.push(item.id)
          }
        })
      }
      fn(row)
      // console.log()
      this.defkeys = keys
    },
    setRightDialogClosed () {
      this.defkeys = []
    },
    async alloRight () {
      var arr = [...this.$refs.mytree.getCheckedKeys(), ...this.$refs.mytree.getHalfCheckedKeys()]
      var rids = arr.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids })
      if (res.meta.status !== 200) return this.$message.error('分配权限失败')
      this.$message.success('分配权限成功')
      this.getRolesList()
      this.setRightdialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.el-tag {
  margin: 20px;
}
</style>
